package com.bootdo.system.dao;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import org.apache.ibatis.annotations.Param;

import com.bootdo.common.annotation.Table;
import com.bootdo.common.annotation.Column;

public class BaseSqlProvider {

	public String get(@Param("c")Class c,@Param("id")Long id) {
		String sql = "";
		String tableName = "";
		 boolean clzHasAnno = c.isAnnotationPresent(Table.class);  
		 if(clzHasAnno) {
			 Annotation anno = c.getAnnotation(Table.class);
			 tableName = ((Table)anno).code();
			 sql  =  "select * from " + tableName + " where id=#{id}";
		 }else {
			 return "";
		 }
		return sql;
	}
	
	public String delete(@Param("c")Class c,@Param("id")Long id) {
		String sql = "";
		String tableName = "";
		 boolean clzHasAnno = c.isAnnotationPresent(Table.class);  
		 if(clzHasAnno) {
			 Annotation anno = c.getAnnotation(Table.class);
			 tableName = ((Table)anno).code();
			 sql  =  "delete from " + tableName + " where id=#{id}";
		 }else {
			 return "";
		 }
		return sql;
	}
	
	public String list(Object obj,@Param("orderBy")String orderBy) throws IllegalArgumentException, IllegalAccessException {
		StringBuffer sql = new StringBuffer("");
		String tableName = "";
		Class c = obj.getClass();
		boolean clzHasAnno = c.isAnnotationPresent(Table.class);  
		if(clzHasAnno) {
			 Annotation anno = c.getAnnotation(Table.class);
			 tableName = ((Table)anno).code();
			 sql.append("select * from ").append(tableName).append(" where 1=1 ");
			 Field[] fields = c.getDeclaredFields();
			 for(Field f :fields) {
				 f.setAccessible(true); 
		         Object val = f.get(obj); 
		         Annotation annoCol = f.getAnnotation(Column.class);
		         if(val != null) {
		        	 sql.append(" and ").append(((Column)annoCol).dbCol()).append("=").append(val).append(" ");
		         }
			 }
			 if(orderBy != null && !"".equals(orderBy)) {
				 sql.append(orderBy).append(" ");
			 }
		 }else {
			 return "";
		 }
		return sql.toString();
	}
	
	public String save(Object obj) throws IllegalArgumentException, IllegalAccessException {
		StringBuffer sql = new StringBuffer("");
		String tableName = "";
		Class c = obj.getClass();
		boolean clzHasAnno = c.isAnnotationPresent(Table.class);  
		if(clzHasAnno) {
			 Annotation anno = c.getAnnotation(Table.class);
			 tableName = ((Table)anno).code();
			 sql.append("INSERT INTO ").append(tableName).append("  ");
			 StringBuffer flds = new StringBuffer("(");
			 StringBuffer vals = new StringBuffer("(");
			 Field[] fields = c.getDeclaredFields();
			 if(fields != null && fields.length>0) {
				 for(Field f :fields) {
					 f.setAccessible(true); 
			         Object val = f.get(obj); 
			         Annotation annoCol = f.getAnnotation(Column.class);
			         flds.append(((Column)annoCol).dbCol()).append(",");
			         vals.append("#{").append(f.getName()).append("},");
				 }
				 sql.append(flds.substring(0, flds.length()-1))
				 .append(") VALUES ")
				 .append(vals.substring(0, vals.length()-1))
				 .append(")");
			 }else {
				 return "";
			 }
		 }else {
			 return "";
		 }
		return sql.toString();
	}
	
	public String update(Object obj) throws IllegalArgumentException, IllegalAccessException {
		StringBuffer sql = new StringBuffer("");
		String tableName = "";
		Class c = obj.getClass();
		boolean clzHasAnno = c.isAnnotationPresent(Table.class);  
		if(clzHasAnno) {
			 Annotation anno = c.getAnnotation(Table.class);
			 tableName = ((Table)anno).code();
			 sql.append("UPDATE ").append(tableName).append(" SET ");
			 StringBuffer flds = new StringBuffer("");
			 StringBuffer vals = new StringBuffer("");
			 Field[] fields = c.getDeclaredFields();
			 if(fields != null && fields.length>0) {
				 for(Field f :fields) {
					 f.setAccessible(true); 
			         Object val = f.get(obj); 
			         Annotation annoCol = f.getAnnotation(Column.class);
			         sql.append(((Column)annoCol).dbCol()).append("=");
			         sql.append("#{").append(f.getName()).append("},");
				 }
				 return sql.substring(0, sql.length()-1) + " WHERE id=#{id}";
			 }else {
				 return "";
			 }
		 }else {
			 return "";
		 }
	}
	
}
